package lumingweihua.future.cn.lumingweihua.ui.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import butterknife.BindView;
import butterknife.ButterKnife;
import lumingweihua.future.cn.lumingweihua.R;
import lumingweihua.future.cn.lumingweihua.network.LoadData;
import lumingweihua.future.cn.lumingweihua.network.LoadingHelper;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.InfoDetailData;

/**
 * Created by z1986 on 2018/3/19.
 */

public class InfoDetailActivity extends BaseCompatActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.layout_content)
    ViewGroup layoutContent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_info_detail);
        ButterKnife.bind(this);
        String id = getIntent().getStringExtra(_EXTRA_String_ID);
        LoadData<InfoDetailData> loadData = new LoadData<>(LoadData.Api.资讯详情, this);
        loadData._setOnLoadingListener(new LoadingHelper<InfoDetailData>(layoutContent, loadData) {
            @Override
            public void __onComplete(HttpRequest<Object> request, IHttpResult<InfoDetailData> data) {
                tvTitle.setText(data.getData().title);
                tvDate.setText(data.getData().create_time);
                if (!TextUtils.isEmpty(data.getData().video)) {
                    webView.loadDataWithBaseURL(null, "<html><body><div style='width:100%; position:relative; padding-bottom:56.25%;'><video src='" + data.getData().video + "' style='position: absolute; top:0; left: 0; width: 100%; height: 100%' controls='controls' poster='"+data.getData().img+"' autoplay='autoplay' /></div></body></html>", "text/html", "utf8", null);
                } else if (!TextUtils.isEmpty(data.getData().content)) {
                    webView.loadDataWithBaseURL(null, data.getData().content, "text/html", "utf8", null);
                }
            }
        });
        loadData._loadData(id);
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        webView.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }
}
